{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "1e607c76-af1f-4153-947f-d9aa799f12db",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.基本数据类型：整数、浮点数、布尔值、字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0ad73d19-7923-4362-af38-bdbc575ba5c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = 3\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3c30a982-9956-4ab9-9609-91d20b830c99",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 2, 6, 27)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x + 1, x - 1, x * 2, x ** 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "055a7c9b-d280-42aa-bdac-f0dca0855338",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x += 1\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5bf1fa5a-44c7-4f8a-93a7-ad64a5bd8b2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "48"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x *= 2\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3b40d3bd-d7e1-4906-a1f6-e950bab28529",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(float, int)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = 2.5\n",
    "type(y), type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9be78a85-1581-4f44-92b1-f40ab7d517fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(False, True, True, False)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = True\n",
    "F = False\n",
    "t and F, t or F, not F, t == F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8093db0f-e51f-4917-8697-3516b01817f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e18d2964-c811-4b7d-b6ce-d9ed888ed537",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = \"i love you\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fdf5accb-3afa-466f-af7c-dd2000ffe1e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I LOVE YOU'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.upper() # 还有很多方法，此处不一一赘述"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2f68e83b-8d4f-4170-a16a-80864cd2a3f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.Python数据结构：列表、字典、集合、元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "f0486e9c-7844-469c-b4f5-193a98a3780a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 2, 1]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表\n",
    "l = [3, 2, 1]\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "8fae0551-27cd-4108-8393-607682597b14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "ba69c640-1b99-4e4f-811d-d347767a8226",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 2, 1, 4]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.append(4)\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "b3d1ab1f-1333-4e69-a78b-ad44be640285",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 2, 1]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.pop()\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "43a8bcf9-dd16-43e0-ac3f-5c5e9d53d9c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 2, 1, 'I LOVE YOU', 'I LOVE YOU']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.append(\"I LOVE YOU\")\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "53729be8-2ed4-4672-ab71-cc67f59d4a1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 1, 'I LOVE YOU', 'I LOVE YOU']"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "912fc460-1973-4c73-9cad-4073fad32144",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "2\n",
      "1\n",
      "I LOVE YOU\n",
      "I LOVE YOU\n"
     ]
    }
   ],
   "source": [
    "for x in l:\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "2f263fba-b99c-4706-8547-6bc1dc1e371e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 is 3\n",
      "1 is 2\n",
      "2 is 1\n",
      "3 is I LOVE YOU\n",
      "4 is I LOVE YOU\n"
     ]
    }
   ],
   "source": [
    "for idx, x in enumerate(l):\n",
    "    print(f\"{idx} is {x}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "c29c8de5-a166-4fb0-bcb7-35069f928490",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 4, 1]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l1 = [x ** 2 for x in l]\n",
    "l1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "67cdc542-68bd-4338-a1ec-390a0b3295b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 4]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l2 = [x ** 2 for x in l if x >= 2]\n",
    "l2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "a2ce3392-c653-4d7a-8660-749f09cc1516",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典（key-value）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "98518460-5b08-4648-9f08-717b8fc7e67f",
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {\n",
    "    'dog':'pipixia',\n",
    "    'cat':'xiaxiapi',\n",
    "    'fish':'xiapi'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "a80dc6e4-9031-4522-8160-845f3c1d4b2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'pipixia'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d['dog']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "226afa61-91af-48b3-bdfd-1696754ab06f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'N/A'"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.get('dogsds','N/A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "1e20c265-23b5-4dd8-9917-fc238eb34bbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dog\n",
      "cat\n",
      "fish\n"
     ]
    }
   ],
   "source": [
    "for x in d:\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "71cb5f33-d855-47d1-8ed0-16cf77683b51",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dog is pipixia\n",
      "cat is xiaxiapi\n",
      "fish is xiapi\n"
     ]
    }
   ],
   "source": [
    "for key, val in d.items():\n",
    "    print(f\"{key} is {val}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "a2b99fcb-049b-4eb2-b2f2-a1ee6da5bf9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 集合：无序的元素集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "305c5ab6-6cbb-4b36-8c3f-abeeefc606b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 23, 31, 142}"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = {1,23,142,1,31}\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "ca608a3d-ca2c-46e1-a153-78cd3b90e99a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 is 1\n",
      "1 is 23\n",
      "2 is 142\n",
      "3 is 31\n"
     ]
    }
   ],
   "source": [
    "for idx, animal in enumerate(s):\n",
    "    print(f\"{idx} is {animal}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "0e7adeba-1ccd-4179-9adf-2d3841e5bd40",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 元组：类似于列表，但是不可变，可以作为字典的键和集合元素，列表不可以"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "4c0e7819-95fc-4fc0-8c01-bee7ee3d2ea7",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = (5, 6, 7, 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "2c180f96-9206-4742-8317-74945e7813de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "81b0ab53-0bd7-4baa-8072-e26fcecde30d",
   "metadata": {},
   "outputs": [],
   "source": [
    "d3 = {\n",
    "    t:'I LOVE YOU'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "ca8f2cc8-c30e-497a-a3a2-08c4cd787695",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'I LOVE YOU'"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d3[t]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "2e5a6c4f-c630-41b3-b7ba-0ea91beeef4a",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[72], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m d4 \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m      2\u001b[0m     l:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mI LOVE YOU\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m      3\u001b[0m }\n",
      "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "d4 = {\n",
    "    l:'I LOVE YOU' # list 不可以作为字典的key\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "cb836875-d521-4e72-9a8c-9ddfd8fcf0a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "aa793d7d-9f2e-49ef-b402-e24a652abd86",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sign(x):\n",
    "    if x > 0:\n",
    "        return 'pos'\n",
    "    elif x < 0:\n",
    "        return 'neg'\n",
    "    else:\n",
    "        return 'N/A'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "7cc60b4f-7ab2-456f-ac3b-fc8f39fa3e19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pos\n",
      "pos\n",
      "pos\n",
      "neg\n"
     ]
    }
   ],
   "source": [
    "l.append(-1)\n",
    "for x in l:\n",
    "    print(sign(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "2414a1d1-16cb-4228-94b7-11dd0c5834c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 类与对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "56e1780e-5739-463d-9292-835c1571f008",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Dog:\n",
    "    # 类属性，所有类共享\n",
    "    num = 1\n",
    "    # 初始化方法\n",
    "    def __init__(self, name, age):\n",
    "        # 实例属性，每个对象独有\n",
    "        # self表示该变量指向该对象，没有则无法区分。\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "    # 实例方法\n",
    "    def bark(self):\n",
    "        print(\"bark\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22281695-ecc4-47ef-82e7-e40ef1a462e1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
